package com.banyuan.dao;

import com.banyuan.domain.Types;
import com.banyuan.utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class TypesDao implements PageDao{

    // 查询出所有的types信息

    public static ResultSet selectAll() throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from types");
        return resultSet;
    }

    // 模糊查询，通过拼sql语句和给参数，来模糊查询
    public static ResultSet selectAll(String sql,String limit,Object ...objects) throws SQLException {
        System.out.println(sql);
        System.out.println(limit);
        ResultSet resultSet = JDBCUtils.executeQuery("select * from types where 1=1 "+sql+limit,objects);
        return resultSet;
    }

    @Override
    public  ResultSet selectCountAll(String sql, Object ...objects) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select count(*) from types where 1=1"+sql,objects);
        return resultSet;
    }



    // 通过名字查询数据库，返回count

    public static ResultSet selectByName (String name) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select count(*) as countSum from types where name=?", name);
        return resultSet;
    }

    // 插入一个types类到数据库里

    public static int insert(Types types) throws SQLException {
        int i = JDBCUtils.executeUpdate("insert into types(name,intro,createTime,updateTime,operator) values(?,?,now(),now(),?)",
                types.getName(), types.getIntro(),types.getOperator());
        return i;
    }

    // 通过id删除一个types信息

    public static int delete(Integer id) throws SQLException {
        int i = JDBCUtils.executeUpdate("delete from types where id = ?", id);
        return i;
    }

    // 通过id查询出一个types的所有信息

    public static ResultSet selectTypesById(Integer id) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from types where id = ?", id);
        return resultSet;

    }

    // 通过传入一个types对象，修改他的name
    public static int update(Types types) throws SQLException {
        int i = JDBCUtils.executeUpdate("update types set name=?,intro=?,updateTime=now(),operator=? where id = ? ",
                types.getName(), types.getIntro(), types.getOperator(), types.getId());
        return i;
    }
}
